<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>5.2 lubridate | 商业数据分析师-R 语言数据处理</title>
  <meta name="description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  <meta name="generator" content="bookdown 0.22 and GitBook 2.6.7" />

  <meta property="og:title" content="5.2 lubridate | 商业数据分析师-R 语言数据处理" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="5.2 lubridate | 商业数据分析师-R 语言数据处理" />
  
  <meta name="twitter:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

<meta name="author" content="Yufei Zhong" />


<meta name="date" content="2021-06-23" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="datetime-base-R.html"/>
<link rel="next" href="datetime-application.html"/>
<script src="libs/header-attrs-2.8/header-attrs.js"></script>
<script src="libs/jquery-3.5.1/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<link href="libs/tabwid-1.0.0/tabwid.css" rel="stylesheet" />
<script src="libs/htmlwidgets-1.5.3/htmlwidgets.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.18/datatables.js"></script>
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.10.20/js/jquery.dataTables.min.js"></script>
<link href="libs/crosstalk-1.1.1/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk-1.1.1/js/crosstalk.min.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>


<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">R学习笔记</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a>
<ul>
<li class="chapter" data-level="" data-path="abstract.html"><a href="abstract.html"><i class="fa fa-check"></i>内容概要</a></li>
<li class="chapter" data-level="" data-path="sec-licenses.html"><a href="sec-licenses.html"><i class="fa fa-check"></i>授权说明</a></li>
<li class="chapter" data-level="" data-path="session.html"><a href="session.html"><i class="fa fa-check"></i>运行信息</a></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>关于本人</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="data-read-write-description.html"><a href="data-read-write-description.html"><i class="fa fa-check"></i><b>1</b> 数据导入导出</a>
<ul>
<li class="chapter" data-level="1.1" data-path="readxl-description.html"><a href="readxl-description.html"><i class="fa fa-check"></i><b>1.1</b> readxl</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="readxl-description.html"><a href="readxl-description.html#readxl:install"><i class="fa fa-check"></i><b>1.1.1</b> 安装</a></li>
<li class="chapter" data-level="1.1.2" data-path="readxl-description.html"><a href="readxl-description.html#readxl:usage"><i class="fa fa-check"></i><b>1.1.2</b> 用法</a></li>
<li class="chapter" data-level="1.1.3" data-path="readxl-description.html"><a href="readxl-description.html#batch:read"><i class="fa fa-check"></i><b>1.1.3</b> 批量读取</a></li>
<li class="chapter" data-level="1.1.4" data-path="readxl-description.html"><a href="readxl-description.html#batch:write"><i class="fa fa-check"></i><b>1.1.4</b> 批量输出</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="writexl.html"><a href="writexl.html"><i class="fa fa-check"></i><b>1.2</b> writexl</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="writexl.html"><a href="writexl.html#writexl:usage"><i class="fa fa-check"></i><b>1.2.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html"><i class="fa fa-check"></i><b>1.3</b> openxlsx</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:install"><i class="fa fa-check"></i><b>1.3.1</b> 安装</a></li>
<li class="chapter" data-level="1.3.2" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:functions"><i class="fa fa-check"></i><b>1.3.2</b> 基础功能</a></li>
<li class="chapter" data-level="1.3.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html#定制格式输出"><i class="fa fa-check"></i><b>1.3.3</b> 定制格式输出</a></li>
<li class="chapter" data-level="1.3.4" data-path="openxlsx-description.html"><a href="openxlsx-description.html#函数参数"><i class="fa fa-check"></i><b>1.3.4</b> 函数参数</a></li>
<li class="chapter" data-level="1.3.5" data-path="openxlsx-description.html"><a href="openxlsx-description.html#总结"><i class="fa fa-check"></i><b>1.3.5</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="readr-package.html"><a href="readr-package.html"><i class="fa fa-check"></i><b>1.4</b> readr</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="readr-package.html"><a href="readr-package.html#readr:install"><i class="fa fa-check"></i><b>1.4.1</b> 安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="readr-package.html"><a href="readr-package.html#readr:usage"><i class="fa fa-check"></i><b>1.4.2</b> 用法</a></li>
<li class="chapter" data-level="1.4.3" data-path="readr-package.html"><a href="readr-package.html#readr:write-function"><i class="fa fa-check"></i><b>1.4.3</b> 导出功能</a></li>
<li class="chapter" data-level="1.4.4" data-path="readr-package.html"><a href="readr-package.html#总结-1"><i class="fa fa-check"></i><b>1.4.4</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="data-vroom.html"><a href="data-vroom.html"><i class="fa fa-check"></i><b>1.5</b> vroom</a>
<ul>
<li class="chapter" data-level="1.5.1" data-path="data-vroom.html"><a href="data-vroom.html#安装"><i class="fa fa-check"></i><b>1.5.1</b> 安装</a></li>
<li class="chapter" data-level="1.5.2" data-path="data-vroom.html"><a href="data-vroom.html#用法"><i class="fa fa-check"></i><b>1.5.2</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.6" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html"><i class="fa fa-check"></i><b>1.6</b> Rstudio导入</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#rstudio-工具栏导入"><i class="fa fa-check"></i><b>1.6.1</b> rstudio 工具栏导入</a></li>
<li class="chapter" data-level="1.6.2" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#插件导入"><i class="fa fa-check"></i><b>1.6.2</b> 插件导入</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="data-file-path.html"><a href="data-file-path.html"><i class="fa fa-check"></i><b>1.7</b> 文件路径</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="data-file-path.html"><a href="data-file-path.html#指定路径"><i class="fa fa-check"></i><b>1.7.1</b> 指定路径</a></li>
<li class="chapter" data-level="1.7.2" data-path="data-file-path.html"><a href="data-file-path.html#默认路径"><i class="fa fa-check"></i><b>1.7.2</b> 默认路径</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="data-expand.html"><a href="data-expand.html"><i class="fa fa-check"></i><b>1.8</b> 拓展</a>
<ul>
<li class="chapter" data-level="1.8.1" data-path="data-expand.html"><a href="data-expand.html#tidyxl:package"><i class="fa fa-check"></i><b>1.8.1</b> tidyxl</a></li>
<li class="chapter" data-level="1.8.2" data-path="data-expand.html"><a href="data-expand.html#参考资料"><i class="fa fa-check"></i><b>1.8.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Data-Manipulation-dplyr.html"><a href="Data-Manipulation-dplyr.html"><i class="fa fa-check"></i><b>2</b> 数据处理之-dplyr</a>
<ul>
<li class="chapter" data-level="2.1" data-path="dplyr-description.html"><a href="dplyr-description.html"><i class="fa fa-check"></i><b>2.1</b> 前言</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:install-package"><i class="fa fa-check"></i><b>2.1.1</b> 安装</a></li>
<li class="chapter" data-level="2.1.2" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:difference-of-sql"><i class="fa fa-check"></i><b>2.1.2</b> Excel and Sql 类比</a></li>
<li class="chapter" data-level="2.1.3" data-path="dplyr-description.html"><a href="dplyr-description.html#常见问题"><i class="fa fa-check"></i><b>2.1.3</b> 常见问题</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html"><i class="fa fa-check"></i><b>2.2</b> 基础用法</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-filter"><i class="fa fa-check"></i><b>2.2.1</b> filter</a></li>
<li class="chapter" data-level="2.2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-select"><i class="fa fa-check"></i><b>2.2.2</b> select</a></li>
<li class="chapter" data-level="2.2.3" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-rename"><i class="fa fa-check"></i><b>2.2.3</b> rename</a></li>
<li class="chapter" data-level="2.2.4" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-relocate"><i class="fa fa-check"></i><b>2.2.4</b> relocate</a></li>
<li class="chapter" data-level="2.2.5" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-mutate"><i class="fa fa-check"></i><b>2.2.5</b> mutate</a></li>
<li class="chapter" data-level="2.2.6" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-arrange"><i class="fa fa-check"></i><b>2.2.6</b> arrange</a></li>
<li class="chapter" data-level="2.2.7" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-summarise"><i class="fa fa-check"></i><b>2.2.7</b> summarise</a></li>
<li class="chapter" data-level="2.2.8" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-groupby"><i class="fa fa-check"></i><b>2.2.8</b> group_by</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html"><i class="fa fa-check"></i><b>2.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="dplyr-functions.html"><a href="dplyr-functions.html#条件判断"><i class="fa fa-check"></i><b>2.3.1</b> 条件判断</a></li>
<li class="chapter" data-level="2.3.2" data-path="dplyr-functions.html"><a href="dplyr-functions.html#case_when"><i class="fa fa-check"></i><b>2.3.2</b> case_when</a></li>
<li class="chapter" data-level="2.3.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html#计数函数"><i class="fa fa-check"></i><b>2.3.3</b> 计数函数</a></li>
<li class="chapter" data-level="2.3.4" data-path="dplyr-functions.html"><a href="dplyr-functions.html#排序函数"><i class="fa fa-check"></i><b>2.3.4</b> 排序函数</a></li>
<li class="chapter" data-level="2.3.5" data-path="dplyr-functions.html"><a href="dplyr-functions.html#提取向量"><i class="fa fa-check"></i><b>2.3.5</b> 提取向量</a></li>
<li class="chapter" data-level="2.3.6" data-path="dplyr-functions.html"><a href="dplyr-functions.html#dplyr:slice"><i class="fa fa-check"></i><b>2.3.6</b> slice 系列</a></li>
<li class="chapter" data-level="2.3.7" data-path="dplyr-functions.html"><a href="dplyr-functions.html#group-系列"><i class="fa fa-check"></i><b>2.3.7</b> group 系列</a></li>
<li class="chapter" data-level="2.3.8" data-path="dplyr-functions.html"><a href="dplyr-functions.html#其它函数"><i class="fa fa-check"></i><b>2.3.8</b> 其它函数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html"><i class="fa fa-check"></i><b>2.4</b> 表关联</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-table-usage"><i class="fa fa-check"></i><b>2.4.1</b> 两表关联</a></li>
<li class="chapter" data-level="2.4.2" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-multiple-table"><i class="fa fa-check"></i><b>2.4.2</b> 多表操作</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html"><i class="fa fa-check"></i><b>2.5</b> 分组操作</a>
<ul>
<li class="chapter" data-level="2.5.1" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#添加分组"><i class="fa fa-check"></i><b>2.5.1</b> 添加分组</a></li>
<li class="chapter" data-level="2.5.2" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#查看分组"><i class="fa fa-check"></i><b>2.5.2</b> 查看分组</a></li>
<li class="chapter" data-level="2.5.3" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#更改和添加分组变量"><i class="fa fa-check"></i><b>2.5.3</b> 更改和添加分组变量</a></li>
<li class="chapter" data-level="2.5.4" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#删除分组变量"><i class="fa fa-check"></i><b>2.5.4</b> 删除分组变量</a></li>
<li class="chapter" data-level="2.5.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#动词影响"><i class="fa fa-check"></i><b>2.5.5</b> 动词影响</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html"><i class="fa fa-check"></i><b>2.6</b> 列操作</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#dplyr-column-manipulation-basic-usage"><i class="fa fa-check"></i><b>2.6.1</b> 基础用法</a></li>
<li class="chapter" data-level="2.6.2" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#多种函数功能"><i class="fa fa-check"></i><b>2.6.2</b> 多种函数功能</a></li>
<li class="chapter" data-level="2.6.3" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#当前列"><i class="fa fa-check"></i><b>2.6.3</b> 当前列</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html"><i class="fa fa-check"></i><b>2.7</b> 行操作</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#比较差异"><i class="fa fa-check"></i><b>2.7.1</b> 比较差异</a></li>
<li class="chapter" data-level="2.7.2" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#常用案例"><i class="fa fa-check"></i><b>2.7.2</b> 常用案例</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="dplyr-programming.html"><a href="dplyr-programming.html"><i class="fa fa-check"></i><b>2.8</b> <code>dplyr</code>编程</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="dplyr-programming.html"><a href="dplyr-programming.html#案例"><i class="fa fa-check"></i><b>2.8.1</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="dplyr-reference-material.html"><a href="dplyr-reference-material.html"><i class="fa fa-check"></i><b>2.9</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Data-tidyr-package.html"><a href="Data-tidyr-package.html"><i class="fa fa-check"></i><b>3</b> 整洁数据之 tidyr</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html"><i class="fa fa-check"></i><b>3.1</b> tidyr 介绍</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:install"><i class="fa fa-check"></i><b>3.1.1</b> 安装</a></li>
<li class="chapter" data-level="3.1.2" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:functions"><i class="fa fa-check"></i><b>3.1.2</b> 主要功能</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="pivoting.html"><a href="pivoting.html"><i class="fa fa-check"></i><b>3.2</b> 长宽转换</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="pivoting.html"><a href="pivoting.html#宽转长"><i class="fa fa-check"></i><b>3.2.1</b> 宽转长</a></li>
<li class="chapter" data-level="3.2.2" data-path="pivoting.html"><a href="pivoting.html#用法-1"><i class="fa fa-check"></i><b>3.2.2</b> 用法</a></li>
<li class="chapter" data-level="3.2.3" data-path="pivoting.html"><a href="pivoting.html#长转宽"><i class="fa fa-check"></i><b>3.2.3</b> 长转宽</a></li>
<li class="chapter" data-level="3.2.4" data-path="pivoting.html"><a href="pivoting.html#用法-2"><i class="fa fa-check"></i><b>3.2.4</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="tidyr-rectangle.html"><a href="tidyr-rectangle.html"><i class="fa fa-check"></i><b>3.3</b> 矩形数据</a></li>
<li class="chapter" data-level="3.4" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html"><i class="fa fa-check"></i><b>3.4</b> 嵌套数据</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#基础"><i class="fa fa-check"></i><b>3.4.1</b> 基础</a></li>
<li class="chapter" data-level="3.4.2" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#嵌套数据和模型"><i class="fa fa-check"></i><b>3.4.2</b> 嵌套数据和模型</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="character-vector.html"><a href="character-vector.html"><i class="fa fa-check"></i><b>3.5</b> 字符处理</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="character-vector.html"><a href="character-vector.html#新列提取"><i class="fa fa-check"></i><b>3.5.1</b> 新列提取</a></li>
<li class="chapter" data-level="3.5.2" data-path="character-vector.html"><a href="character-vector.html#拆分"><i class="fa fa-check"></i><b>3.5.2</b> 拆分</a></li>
<li class="chapter" data-level="3.5.3" data-path="character-vector.html"><a href="character-vector.html#列拆分行"><i class="fa fa-check"></i><b>3.5.3</b> 列拆分行</a></li>
<li class="chapter" data-level="3.5.4" data-path="character-vector.html"><a href="character-vector.html#合并多列"><i class="fa fa-check"></i><b>3.5.4</b> 合并多列</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="tidyr-expand.html"><a href="tidyr-expand.html"><i class="fa fa-check"></i><b>3.6</b> 扩展数据框</a></li>
<li class="chapter" data-level="3.7" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html"><i class="fa fa-check"></i><b>3.7</b> 缺失值</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#replace_na"><i class="fa fa-check"></i><b>3.7.1</b> replace_na</a></li>
<li class="chapter" data-level="3.7.2" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#fill"><i class="fa fa-check"></i><b>3.7.2</b> fill</a></li>
<li class="chapter" data-level="3.7.3" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#drop_na"><i class="fa fa-check"></i><b>3.7.3</b> drop_na</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="character-handling.html"><a href="character-handling.html"><i class="fa fa-check"></i><b>4</b> 字符处理</a>
<ul>
<li class="chapter" data-level="4.1" data-path="character-base-R.html"><a href="character-base-R.html"><i class="fa fa-check"></i><b>4.1</b> base R</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="character-base-R.html"><a href="character-base-R.html#单双引号"><i class="fa fa-check"></i><b>4.1.1</b> 单双引号</a></li>
<li class="chapter" data-level="4.1.2" data-path="character-base-R.html"><a href="character-base-R.html#转义"><i class="fa fa-check"></i><b>4.1.2</b> 转义</a></li>
<li class="chapter" data-level="4.1.3" data-path="character-base-R.html"><a href="character-base-R.html#常用函数"><i class="fa fa-check"></i><b>4.1.3</b> 常用函数</a></li>
<li class="chapter" data-level="4.1.4" data-path="character-base-R.html"><a href="character-base-R.html#newfeatures"><i class="fa fa-check"></i><b>4.1.4</b> 新特性</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html"><i class="fa fa-check"></i><b>4.2</b> stringr</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-install"><i class="fa fa-check"></i><b>4.2.1</b> 安装</a></li>
<li class="chapter" data-level="4.2.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-usage"><i class="fa fa-check"></i><b>4.2.2</b> 基本使用</a></li>
<li class="chapter" data-level="4.2.3" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-functions"><i class="fa fa-check"></i><b>4.2.3</b> 常用函数</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="character-application.html"><a href="character-application.html"><i class="fa fa-check"></i><b>4.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="character-application.html"><a href="character-application.html#实现excel函数"><i class="fa fa-check"></i><b>4.3.1</b> 实现excel函数</a></li>
<li class="chapter" data-level="4.3.2" data-path="character-application.html"><a href="character-application.html#使用案例"><i class="fa fa-check"></i><b>4.3.2</b> 使用案例</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="character-the-difference-stringr-and-base.html"><a href="character-the-difference-stringr-and-base.html"><i class="fa fa-check"></i><b>4.4</b> base 和 stringr</a></li>
<li class="chapter" data-level="4.5" data-path="character-reference-material.html"><a href="character-reference-material.html"><i class="fa fa-check"></i><b>4.5</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="datetime.html"><a href="datetime.html"><i class="fa fa-check"></i><b>5</b> 时间处理</a>
<ul>
<li class="chapter" data-level="5.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html"><i class="fa fa-check"></i><b>5.1</b> base R</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-date-class"><i class="fa fa-check"></i><b>5.1.1</b> Date</a></li>
<li class="chapter" data-level="5.1.2" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXct-class"><i class="fa fa-check"></i><b>5.1.2</b> POSIXct</a></li>
<li class="chapter" data-level="5.1.3" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXlt-class"><i class="fa fa-check"></i><b>5.1.3</b> POSIXlt</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="lubridate.html"><a href="lubridate.html"><i class="fa fa-check"></i><b>5.2</b> lubridate</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="lubridate.html"><a href="lubridate.html#lubridate-install"><i class="fa fa-check"></i><b>5.2.1</b> 安装包</a></li>
<li class="chapter" data-level="5.2.2" data-path="lubridate.html"><a href="lubridate.html#get-current-datetime"><i class="fa fa-check"></i><b>5.2.2</b> 当前时间日期</a></li>
<li class="chapter" data-level="5.2.3" data-path="lubridate.html"><a href="lubridate.html#make-datetime"><i class="fa fa-check"></i><b>5.2.3</b> 构造日期时间</a></li>
<li class="chapter" data-level="5.2.4" data-path="lubridate.html"><a href="lubridate.html#parse-datetime"><i class="fa fa-check"></i><b>5.2.4</b> 解析日期时间</a></li>
<li class="chapter" data-level="5.2.5" data-path="lubridate.html"><a href="lubridate.html#extracting-datetime-information"><i class="fa fa-check"></i><b>5.2.5</b> 提取日期时间成分</a></li>
<li class="chapter" data-level="5.2.6" data-path="lubridate.html"><a href="lubridate.html#time-zones"><i class="fa fa-check"></i><b>5.2.6</b> 处理时区</a></li>
<li class="chapter" data-level="5.2.7" data-path="lubridate.html"><a href="lubridate.html#interveal"><i class="fa fa-check"></i><b>5.2.7</b> 时间间隔</a></li>
<li class="chapter" data-level="5.2.8" data-path="lubridate.html"><a href="lubridate.html#calculate-datetime"><i class="fa fa-check"></i><b>5.2.8</b> 时间日期计算</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="datetime-application.html"><a href="datetime-application.html"><i class="fa fa-check"></i><b>5.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="datetime-application.html"><a href="datetime-application.html#日报同环比计算"><i class="fa fa-check"></i><b>5.3.1</b> 日报同环比计算</a></li>
<li class="chapter" data-level="5.3.2" data-path="datetime-application.html"><a href="datetime-application.html#清洗不同类型日期格式"><i class="fa fa-check"></i><b>5.3.2</b> 清洗不同类型日期格式</a></li>
<li class="chapter" data-level="5.3.3" data-path="datetime-application.html"><a href="datetime-application.html#扫码后中奖时间匹配"><i class="fa fa-check"></i><b>5.3.3</b> 扫码后中奖时间匹配</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html"><i class="fa fa-check"></i><b>5.4</b> 补充资料</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#excel-and-r"><i class="fa fa-check"></i><b>5.4.1</b> Excel and R</a></li>
<li class="chapter" data-level="5.4.2" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#参考资料-1"><i class="fa fa-check"></i><b>5.4.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="forcats.html"><a href="forcats.html"><i class="fa fa-check"></i><b>6</b> 因子介绍</a>
<ul>
<li class="chapter" data-level="6.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html"><i class="fa fa-check"></i><b>6.1</b> 介绍</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html#forcats:install"><i class="fa fa-check"></i><b>6.1.1</b> 安装</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="forcats-usage.html"><a href="forcats-usage.html"><i class="fa fa-check"></i><b>6.2</b> 用法</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="forcats-usage.html"><a href="forcats-usage.html#创建因子"><i class="fa fa-check"></i><b>6.2.1</b> 创建因子</a></li>
<li class="chapter" data-level="6.2.2" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子顺序"><i class="fa fa-check"></i><b>6.2.2</b> 修改因子顺序</a></li>
<li class="chapter" data-level="6.2.3" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子级别"><i class="fa fa-check"></i><b>6.2.3</b> 修改因子级别</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Data-Manipulation-datatable.html"><a href="Data-Manipulation-datatable.html"><i class="fa fa-check"></i><b>7</b> data.table</a>
<ul>
<li class="chapter" data-level="7.1" data-path="datatable-description.html"><a href="datatable-description.html"><i class="fa fa-check"></i><b>7.1</b> 基础介绍</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="datatable-description.html"><a href="datatable-description.html#读取数据"><i class="fa fa-check"></i><b>7.1.1</b> 读取数据</a></li>
<li class="chapter" data-level="7.1.2" data-path="datatable-description.html"><a href="datatable-description.html#基本格式"><i class="fa fa-check"></i><b>7.1.2</b> 基本格式</a></li>
<li class="chapter" data-level="7.1.3" data-path="datatable-description.html"><a href="datatable-description.html#i-j-by-使用"><i class="fa fa-check"></i><b>7.1.3</b> i j by 使用</a></li>
<li class="chapter" data-level="7.1.4" data-path="datatable-description.html"><a href="datatable-description.html#行列筛选总结"><i class="fa fa-check"></i><b>7.1.4</b> 行列筛选总结</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="datatable-usage.html"><a href="datatable-usage.html"><i class="fa fa-check"></i><b>7.2</b> 常规操作</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="datatable-usage.html"><a href="datatable-usage.html#行筛选"><i class="fa fa-check"></i><b>7.2.1</b> 行筛选</a></li>
<li class="chapter" data-level="7.2.2" data-path="datatable-usage.html"><a href="datatable-usage.html#新增更新列"><i class="fa fa-check"></i><b>7.2.2</b> 新增更新列</a></li>
<li class="chapter" data-level="7.2.3" data-path="datatable-usage.html"><a href="datatable-usage.html#排序"><i class="fa fa-check"></i><b>7.2.3</b> 排序</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="datatable-function.html"><a href="datatable-function.html"><i class="fa fa-check"></i><b>7.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="datatable-function.html"><a href="datatable-function.html#特殊符号"><i class="fa fa-check"></i><b>7.3.1</b> 特殊符号</a></li>
<li class="chapter" data-level="7.3.2" data-path="datatable-function.html"><a href="datatable-function.html#排序函数-1"><i class="fa fa-check"></i><b>7.3.2</b> 排序函数</a></li>
<li class="chapter" data-level="7.3.3" data-path="datatable-function.html"><a href="datatable-function.html#非重复计数"><i class="fa fa-check"></i><b>7.3.3</b> 非重复计数</a></li>
<li class="chapter" data-level="7.3.4" data-path="datatable-function.html"><a href="datatable-function.html#判断函数"><i class="fa fa-check"></i><b>7.3.4</b> 判断函数</a></li>
<li class="chapter" data-level="7.3.5" data-path="datatable-function.html"><a href="datatable-function.html#交集-差集-合并"><i class="fa fa-check"></i><b>7.3.5</b> 交集 差集 合并</a></li>
<li class="chapter" data-level="7.3.6" data-path="datatable-function.html"><a href="datatable-function.html#长宽转换"><i class="fa fa-check"></i><b>7.3.6</b> 长宽转换</a></li>
<li class="chapter" data-level="7.3.7" data-path="datatable-function.html"><a href="datatable-function.html#表连接"><i class="fa fa-check"></i><b>7.3.7</b> 表连接</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html"><i class="fa fa-check"></i><b>7.4</b> 高级函数</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#groupingsets"><i class="fa fa-check"></i><b>7.4.1</b> groupingsets</a></li>
<li class="chapter" data-level="7.4.2" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#rleid"><i class="fa fa-check"></i><b>7.4.2</b> rleid</a></li>
<li class="chapter" data-level="7.4.3" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#shift"><i class="fa fa-check"></i><b>7.4.3</b> shift</a></li>
<li class="chapter" data-level="7.4.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#j"><i class="fa fa-check"></i><b>7.4.4</b> J</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="datatable-tips.html"><a href="datatable-tips.html"><i class="fa fa-check"></i><b>7.5</b> 小技巧</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="datatable-tips.html"><a href="datatable-tips.html#用抑制中间过程输出"><i class="fa fa-check"></i><b>7.5.1</b> 用{}抑制中间过程输出</a></li>
<li class="chapter" data-level="7.5.2" data-path="datatable-tips.html"><a href="datatable-tips.html#使用打印data.table"><i class="fa fa-check"></i><b>7.5.2</b> 使用[]打印data.table</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="datatable-application.html"><a href="datatable-application.html"><i class="fa fa-check"></i><b>7.6</b> 运用</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="datatable-application.html"><a href="datatable-application.html#自定义函数计算"><i class="fa fa-check"></i><b>7.6.1</b> 自定义函数计算</a></li>
<li class="chapter" data-level="7.6.2" data-path="datatable-application.html"><a href="datatable-application.html#带汇总的聚合运算"><i class="fa fa-check"></i><b>7.6.2</b> 带汇总的聚合运算</a></li>
<li class="chapter" data-level="7.6.3" data-path="datatable-application.html"><a href="datatable-application.html#行列转变"><i class="fa fa-check"></i><b>7.6.3</b> 行列转变</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="database.html"><a href="database.html"><i class="fa fa-check"></i><b>8</b> database</a>
<ul>
<li class="chapter" data-level="8.1" data-path="database-install.html"><a href="database-install.html"><i class="fa fa-check"></i><b>8.1</b> 安装数据库</a></li>
<li class="chapter" data-level="8.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html"><i class="fa fa-check"></i><b>8.2</b> odbc包</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="database-odbc-package.html"><a href="database-odbc-package.html#安装包"><i class="fa fa-check"></i><b>8.2.1</b> 安装包</a></li>
<li class="chapter" data-level="8.2.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html#驱动安装"><i class="fa fa-check"></i><b>8.2.2</b> 驱动安装</a></li>
<li class="chapter" data-level="8.2.3" data-path="database-odbc-package.html"><a href="database-odbc-package.html#连接数据库"><i class="fa fa-check"></i><b>8.2.3</b> 连接数据库</a></li>
<li class="chapter" data-level="8.2.4" data-path="database-odbc-package.html"><a href="database-odbc-package.html#读写数据库"><i class="fa fa-check"></i><b>8.2.4</b> 读写数据库</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html"><i class="fa fa-check"></i><b>8.3</b> DBI包</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="database-DBI-package.html"><a href="database-DBI-package.html#安装-1"><i class="fa fa-check"></i><b>8.3.1</b> 安装</a></li>
<li class="chapter" data-level="8.3.2" data-path="database-DBI-package.html"><a href="database-DBI-package.html#连接数据库-1"><i class="fa fa-check"></i><b>8.3.2</b> 连接数据库</a></li>
<li class="chapter" data-level="8.3.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html#读写数据库-1"><i class="fa fa-check"></i><b>8.3.3</b> 读写数据库</a></li>
<li class="chapter" data-level="8.3.4" data-path="database-DBI-package.html"><a href="database-DBI-package.html#其它函数介绍"><i class="fa fa-check"></i><b>8.3.4</b> 其它函数介绍</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="database-else-package.html"><a href="database-else-package.html"><i class="fa fa-check"></i><b>8.4</b> 其他R包</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="database-else-package.html"><a href="database-else-package.html#rodbc包"><i class="fa fa-check"></i><b>8.4.1</b> RODBC包</a></li>
<li class="chapter" data-level="8.4.2" data-path="database-else-package.html"><a href="database-else-package.html#roracle包"><i class="fa fa-check"></i><b>8.4.2</b> ROracle包</a></li>
<li class="chapter" data-level="8.4.3" data-path="database-else-package.html"><a href="database-else-package.html#rmysql包"><i class="fa fa-check"></i><b>8.4.3</b> RMySQL包</a></li>
<li class="chapter" data-level="8.4.4" data-path="database-else-package.html"><a href="database-else-package.html#rsqlite"><i class="fa fa-check"></i><b>8.4.4</b> RSQLite</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="database-big-data.html"><a href="database-big-data.html"><i class="fa fa-check"></i><b>8.5</b> 大数据相关R包</a>
<ul>
<li class="chapter" data-level="8.5.1" data-path="database-big-data.html"><a href="database-big-data.html#sparklyr"><i class="fa fa-check"></i><b>8.5.1</b> sparklyr</a></li>
<li class="chapter" data-level="8.5.2" data-path="database-big-data.html"><a href="database-big-data.html#RClickhouse"><i class="fa fa-check"></i><b>8.5.2</b> RClickhouse</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="database-FAQ.html"><a href="database-FAQ.html"><i class="fa fa-check"></i><b>8.6</b> 常见问题</a>
<ul>
<li class="chapter" data-level="8.6.1" data-path="database-FAQ.html"><a href="database-FAQ.html#乱码问题"><i class="fa fa-check"></i><b>8.6.1</b> 乱码问题</a></li>
<li class="chapter" data-level="8.6.2" data-path="database-FAQ.html"><a href="database-FAQ.html#无法连接问题"><i class="fa fa-check"></i><b>8.6.2</b> 无法连接问题</a></li>
<li class="chapter" data-level="8.6.3" data-path="database-FAQ.html"><a href="database-FAQ.html#远程连接"><i class="fa fa-check"></i><b>8.6.3</b> 远程连接</a></li>
<li class="chapter" data-level="8.6.4" data-path="database-FAQ.html"><a href="database-FAQ.html#跳板机连接"><i class="fa fa-check"></i><b>8.6.4</b> 跳板机连接</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html"><i class="fa fa-check"></i><b>8.7</b> dbplyr</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#安装-2"><i class="fa fa-check"></i><b>8.7.1</b> 安装</a></li>
<li class="chapter" data-level="8.7.2" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#基础用法-1"><i class="fa fa-check"></i><b>8.7.2</b> 基础用法</a></li>
<li class="chapter" data-level="8.7.3" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#无法正确转化"><i class="fa fa-check"></i><b>8.7.3</b> 无法正确转化</a></li>
</ul></li>
<li class="chapter" data-level="8.8" data-path="database-reference-material.html"><a href="database-reference-material.html"><i class="fa fa-check"></i><b>8.8</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="R-loop-structure.html"><a href="R-loop-structure.html"><i class="fa fa-check"></i><b>9</b> 循环结构</a>
<ul>
<li class="chapter" data-level="9.1" data-path="R-loop-description.html"><a href="R-loop-description.html"><i class="fa fa-check"></i><b>9.1</b> R循环介绍</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="R-loop-description.html"><a href="R-loop-description.html#R:loop-example"><i class="fa fa-check"></i><b>9.1.1</b> 简单示例</a></li>
<li class="chapter" data-level="9.1.2" data-path="R-loop-description.html"><a href="R-loop-description.html#three:loop-structure"><i class="fa fa-check"></i><b>9.1.2</b> 循环结构</a></li>
<li class="chapter" data-level="9.1.3" data-path="R-loop-description.html"><a href="R-loop-description.html#loop:control"><i class="fa fa-check"></i><b>9.1.3</b> 循环控制</a></li>
<li class="chapter" data-level="9.1.4" data-path="R-loop-description.html"><a href="R-loop-description.html#嵌套循环"><i class="fa fa-check"></i><b>9.1.4</b> 嵌套循环</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="loop-changes.html"><a href="loop-changes.html"><i class="fa fa-check"></i><b>9.2</b> 循环变化</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="loop-changes.html"><a href="loop-changes.html#修改已有对象"><i class="fa fa-check"></i><b>9.2.1</b> 修改已有对象</a></li>
<li class="chapter" data-level="9.2.2" data-path="loop-changes.html"><a href="loop-changes.html#循环模式"><i class="fa fa-check"></i><b>9.2.2</b> 循环模式</a></li>
<li class="chapter" data-level="9.2.3" data-path="loop-changes.html"><a href="loop-changes.html#未知长度输出"><i class="fa fa-check"></i><b>9.2.3</b> 未知长度输出</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="apply-function.html"><a href="apply-function.html"><i class="fa fa-check"></i><b>9.3</b> apply系列函数</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="apply-function.html"><a href="apply-function.html#lapply"><i class="fa fa-check"></i><b>9.3.1</b> lapply 循环迭代</a></li>
<li class="chapter" data-level="9.3.2" data-path="apply-function.html"><a href="apply-function.html#sapply"><i class="fa fa-check"></i><b>9.3.2</b> sapply</a></li>
<li class="chapter" data-level="9.3.3" data-path="apply-function.html"><a href="apply-function.html#vapply"><i class="fa fa-check"></i><b>9.3.3</b> vapply 迭代安全函数</a></li>
<li class="chapter" data-level="9.3.4" data-path="apply-function.html"><a href="apply-function.html#apply"><i class="fa fa-check"></i><b>9.3.4</b> apply 多维数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="purrr-Iteration.html"><a href="purrr-Iteration.html"><i class="fa fa-check"></i><b>10</b> 循环迭代purrr介绍</a>
<ul>
<li class="chapter" data-level="10.1" data-path="安装-3.html"><a href="安装-3.html"><i class="fa fa-check"></i><b>10.1</b> 安装</a></li>
<li class="chapter" data-level="10.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html"><i class="fa fa-check"></i><b>10.2</b> 常规用法</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#map-系列"><i class="fa fa-check"></i><b>10.2.1</b> map 系列</a></li>
<li class="chapter" data-level="10.2.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#pmap-系列"><i class="fa fa-check"></i><b>10.2.2</b> pmap 系列</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="purrr-map.html"><a href="purrr-map.html"><i class="fa fa-check"></i><b>10.3</b> map系列函数</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="purrr-map.html"><a href="purrr-map.html#用法-3"><i class="fa fa-check"></i><b>10.3.1</b> 用法</a></li>
<li class="chapter" data-level="10.3.2" data-path="purrr-map.html"><a href="purrr-map.html#案例-5"><i class="fa fa-check"></i><b>10.3.2</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="purrr-map2.html"><a href="purrr-map2.html"><i class="fa fa-check"></i><b>10.4</b> map2 系列函数</a></li>
<li class="chapter" data-level="10.5" data-path="purrr-multi-parameter.html"><a href="purrr-multi-parameter.html"><i class="fa fa-check"></i><b>10.5</b> pmap系列函数</a></li>
<li class="chapter" data-level="10.6" data-path="purrr-reduce.html"><a href="purrr-reduce.html"><i class="fa fa-check"></i><b>10.6</b> 归约累计函数</a>
<ul>
<li class="chapter" data-level="10.6.1" data-path="purrr-reduce.html"><a href="purrr-reduce.html#reduce-函数"><i class="fa fa-check"></i><b>10.6.1</b> reduce 函数</a></li>
<li class="chapter" data-level="10.6.2" data-path="purrr-reduce.html"><a href="purrr-reduce.html#accumulate-函数"><i class="fa fa-check"></i><b>10.6.2</b> accumulate 函数</a></li>
</ul></li>
<li class="chapter" data-level="10.7" data-path="purrr-safely-functions.html"><a href="purrr-safely-functions.html"><i class="fa fa-check"></i><b>10.7</b> 安全函数</a></li>
<li class="chapter" data-level="10.8" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html"><i class="fa fa-check"></i><b>10.8</b> 其他函数介绍</a>
<ul>
<li class="chapter" data-level="10.8.1" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#flatten-系列"><i class="fa fa-check"></i><b>10.8.1</b> flatten 系列</a></li>
<li class="chapter" data-level="10.8.2" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#imap-函数"><i class="fa fa-check"></i><b>10.8.2</b> imap 函数</a></li>
<li class="chapter" data-level="10.8.3" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#walk-系列"><i class="fa fa-check"></i><b>10.8.3</b> walk 系列</a></li>
<li class="chapter" data-level="10.8.4" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#逻辑判断函数"><i class="fa fa-check"></i><b>10.8.4</b> 逻辑判断函数</a></li>
<li class="chapter" data-level="10.8.5" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#提取函数"><i class="fa fa-check"></i><b>10.8.5</b> 提取函数</a></li>
<li class="chapter" data-level="10.8.6" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#筛选函数"><i class="fa fa-check"></i><b>10.8.6</b> 筛选函数</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="self-define-function.html"><a href="self-define-function.html"><i class="fa fa-check"></i><b>11</b> 自定义函数</a>
<ul>
<li class="chapter" data-level="11.1" data-path="define-function-example.html"><a href="define-function-example.html"><i class="fa fa-check"></i><b>11.1</b> 简单示例</a></li>
<li class="chapter" data-level="11.2" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html"><i class="fa fa-check"></i><b>11.2</b> 条件执行</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html#多条件执行"><i class="fa fa-check"></i><b>11.2.1</b> 多条件执行</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html"><i class="fa fa-check"></i><b>11.3</b> 函数参数</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数名称"><i class="fa fa-check"></i><b>11.3.1</b> 参数名称</a></li>
<li class="chapter" data-level="11.3.2" data-path="define-function-parameter.html"><a href="define-function-parameter.html#检查参数值"><i class="fa fa-check"></i><b>11.3.2</b> 检查参数值</a></li>
<li class="chapter" data-level="11.3.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数-5"><i class="fa fa-check"></i><b>11.3.3</b> …参数</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="function-return.html"><a href="function-return.html"><i class="fa fa-check"></i><b>11.4</b> 返回值</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="function-return.html"><a href="function-return.html#显式返回"><i class="fa fa-check"></i><b>11.4.1</b> 显式返回</a></li>
<li class="chapter" data-level="11.4.2" data-path="function-return.html"><a href="function-return.html#编写管道函数"><i class="fa fa-check"></i><b>11.4.2</b> 编写管道函数</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="function-environment.html"><a href="function-environment.html"><i class="fa fa-check"></i><b>11.5</b> 环境</a></li>
<li class="chapter" data-level="11.6" data-path="function-expand.html"><a href="function-expand.html"><i class="fa fa-check"></i><b>11.6</b> 拓展部分</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="file-system-operation.html"><a href="file-system-operation.html"><i class="fa fa-check"></i><b>12</b> R语言文件系统操作</a>
<ul>
<li class="chapter" data-level="12.1" data-path="file-base-r.html"><a href="file-base-r.html"><i class="fa fa-check"></i><b>12.1</b> base R</a>
<ul>
<li class="chapter" data-level="12.1.1" data-path="file-base-r.html"><a href="file-base-r.html#base:r-usage"><i class="fa fa-check"></i><b>12.1.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="fs-package.html"><a href="fs-package.html"><i class="fa fa-check"></i><b>12.2</b> fs package</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="fs-package.html"><a href="fs-package.html#fs-install"><i class="fa fa-check"></i><b>12.2.1</b> 安装</a></li>
<li class="chapter" data-level="12.2.2" data-path="fs-package.html"><a href="fs-package.html#fs-advanced"><i class="fa fa-check"></i><b>12.2.2</b> 优势</a></li>
<li class="chapter" data-level="12.2.3" data-path="fs-package.html"><a href="fs-package.html#fs-usage"><i class="fa fa-check"></i><b>12.2.3</b> 基础用法</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="fs-functions.html"><a href="fs-functions.html"><i class="fa fa-check"></i><b>12.3</b> fs函数介绍</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="fs-functions.html"><a href="fs-functions.html#fs-file-manipulation"><i class="fa fa-check"></i><b>12.3.1</b> 文件操作</a></li>
<li class="chapter" data-level="12.3.2" data-path="fs-functions.html"><a href="fs-functions.html#fs-dir-manipulation"><i class="fa fa-check"></i><b>12.3.2</b> 目录操作</a></li>
<li class="chapter" data-level="12.3.3" data-path="fs-functions.html"><a href="fs-functions.html#fs-link-manipulation"><i class="fa fa-check"></i><b>12.3.3</b> 链接操作</a></li>
<li class="chapter" data-level="12.3.4" data-path="fs-functions.html"><a href="fs-functions.html#fs-path-manipulation"><i class="fa fa-check"></i><b>12.3.4</b> 路径操作</a></li>
<li class="chapter" data-level="12.3.5" data-path="fs-functions.html"><a href="fs-functions.html#fs-help-manipulation"><i class="fa fa-check"></i><b>12.3.5</b> 辅助函数</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="file-base-fs-difference.html"><a href="file-base-fs-difference.html"><i class="fa fa-check"></i><b>12.4</b> base-fs-shell比较</a></li>
<li class="chapter" data-level="12.5" data-path="file-manipulation-reference-material.html"><a href="file-manipulation-reference-material.html"><i class="fa fa-check"></i><b>12.5</b> 参考资料</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://gitee.com/zhongyufei/Rbook" target="blank">gitee地址</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">商业数据分析师-R 语言数据处理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="lubridate" class="section level2" number="5.2">
<h2><span class="header-section-number">5.2</span> lubridate</h2>
<p><code>lubridate</code>包是对Base R中POSIXct类的封装。所以无论从函数名还是功能等方面，lubridate包中的函数功能更加清晰明了。从获取当前日期、时间，解析时间日期中的年、月、日、星期，计算年月间隔天数等常用的时间日期功能，<code>lubridate</code>包中都有相对应的功能函数。</p>
<p>在处理日期时间数据时，我常用<code>lubridate</code>解决，本节将介绍包中部分函数用法。</p>
<div id="lubridate-install" class="section level3" number="5.2.1">
<h3><span class="header-section-number">5.2.1</span> 安装包</h3>
<div class="sourceCode" id="cb329"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb329-1"><a href="lubridate.html#cb329-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;tidyverse&quot;</span>)</span>
<span id="cb329-2"><a href="lubridate.html#cb329-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 仅仅只安装lubridate</span></span>
<span id="cb329-3"><a href="lubridate.html#cb329-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&#39;lubridate&#39;</span>)</span>
<span id="cb329-4"><a href="lubridate.html#cb329-4" aria-hidden="true" tabindex="-1"></a><span class="co"># 开发版</span></span>
<span id="cb329-5"><a href="lubridate.html#cb329-5" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;tidyverse/lubridate&quot;</span>)</span></code></pre></div>
<div class="sourceCode" id="cb330"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb330-1"><a href="lubridate.html#cb330-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 加载包</span></span>
<span id="cb330-2"><a href="lubridate.html#cb330-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(lubridate,<span class="at">warn.conflicts =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
</div>
<div id="get-current-datetime" class="section level3" number="5.2.2">
<h3><span class="header-section-number">5.2.2</span> 当前时间日期</h3>
<ul>
<li><code>now</code>函数</li>
</ul>
<p>now()函数是当前时间，只有一个参数tzone,默认为系统的timezone。</p>
<div class="sourceCode" id="cb331"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb331-1"><a href="lubridate.html#cb331-1" aria-hidden="true" tabindex="-1"></a><span class="fu">now</span>()</span>
<span id="cb331-2"><a href="lubridate.html#cb331-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:26 CST&quot;</span></span>
<span id="cb331-3"><a href="lubridate.html#cb331-3" aria-hidden="true" tabindex="-1"></a><span class="co"># now(tzone = &#39;Asia/Shanghai&#39;)</span></span>
<span id="cb331-4"><a href="lubridate.html#cb331-4" aria-hidden="true" tabindex="-1"></a><span class="co"># base R</span></span>
<span id="cb331-5"><a href="lubridate.html#cb331-5" aria-hidden="true" tabindex="-1"></a>base<span class="sc">::</span><span class="fu">Sys.time</span>()</span>
<span id="cb331-6"><a href="lubridate.html#cb331-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:26 CST&quot;</span></span></code></pre></div>
<ul>
<li><code>today</code>函数</li>
</ul>
<p>时区同样默认为系统的timezone。</p>
<div class="sourceCode" id="cb332"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb332-1"><a href="lubridate.html#cb332-1" aria-hidden="true" tabindex="-1"></a><span class="fu">today</span>(<span class="at">tzone =</span> <span class="st">&#39;Asia/Shanghai&#39;</span>)</span>
<span id="cb332-2"><a href="lubridate.html#cb332-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23&quot;</span></span>
<span id="cb332-3"><a href="lubridate.html#cb332-3" aria-hidden="true" tabindex="-1"></a><span class="co">#base R</span></span>
<span id="cb332-4"><a href="lubridate.html#cb332-4" aria-hidden="true" tabindex="-1"></a>base<span class="sc">::</span><span class="fu">Sys.Date</span>()</span>
<span id="cb332-5"><a href="lubridate.html#cb332-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23&quot;</span></span></code></pre></div>
</div>
<div id="make-datetime" class="section level3" number="5.2.3">
<h3><span class="header-section-number">5.2.3</span> 构造日期时间</h3>
<p>使用数值直接创建日期时间。</p>
<p>函数<code>make_date()</code>和<code>make_datetime()</code>函数默认时区<a href="#fn10" class="footnote-ref" id="fnref10"><sup>10</sup></a>为“UTC”。</p>
<div class="sourceCode" id="cb333"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb333-1"><a href="lubridate.html#cb333-1" aria-hidden="true" tabindex="-1"></a><span class="fu">make_date</span>(<span class="at">year =</span> <span class="dv">2021</span>, <span class="at">month =</span> <span class="dv">5</span>, <span class="at">day =</span> <span class="dv">1</span>, <span class="at">tz =</span> <span class="st">&quot;Asia/Shanghai&quot;</span>)</span>
<span id="cb333-2"><a href="lubridate.html#cb333-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb333-3"><a href="lubridate.html#cb333-3" aria-hidden="true" tabindex="-1"></a><span class="fu">make_datetime</span>(</span>
<span id="cb333-4"><a href="lubridate.html#cb333-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">year =</span> 1970L,</span>
<span id="cb333-5"><a href="lubridate.html#cb333-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">month =</span> 1L,</span>
<span id="cb333-6"><a href="lubridate.html#cb333-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">day =</span> 1L,</span>
<span id="cb333-7"><a href="lubridate.html#cb333-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">hour =</span> 0L,</span>
<span id="cb333-8"><a href="lubridate.html#cb333-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">min =</span> 0L,</span>
<span id="cb333-9"><a href="lubridate.html#cb333-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">sec =</span> <span class="dv">0</span>,</span>
<span id="cb333-10"><a href="lubridate.html#cb333-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">tz =</span> <span class="st">&quot;Asia/Shanghai&quot;</span></span>
<span id="cb333-11"><a href="lubridate.html#cb333-11" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<ul>
<li>make_datetime</li>
</ul>
<div class="sourceCode" id="cb334"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb334-1"><a href="lubridate.html#cb334-1" aria-hidden="true" tabindex="-1"></a><span class="fu">make_datetime</span>(</span>
<span id="cb334-2"><a href="lubridate.html#cb334-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">year =</span> <span class="fu">year</span>(<span class="fu">today</span>()),</span>
<span id="cb334-3"><a href="lubridate.html#cb334-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">month =</span> <span class="fu">month</span>(<span class="fu">today</span>()),</span>
<span id="cb334-4"><a href="lubridate.html#cb334-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">day =</span> <span class="fu">day</span>(<span class="fu">today</span>()),</span>
<span id="cb334-5"><a href="lubridate.html#cb334-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">hour =</span> <span class="fu">hour</span>(<span class="fu">now</span>()),</span>
<span id="cb334-6"><a href="lubridate.html#cb334-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">min =</span> <span class="fu">minute</span>(<span class="fu">now</span>()),</span>
<span id="cb334-7"><a href="lubridate.html#cb334-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">sec =</span> <span class="fu">second</span>(<span class="fu">now</span>()),</span>
<span id="cb334-8"><a href="lubridate.html#cb334-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span></span>
<span id="cb334-9"><a href="lubridate.html#cb334-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb334-10"><a href="lubridate.html#cb334-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:26 CST&quot;</span></span></code></pre></div>
<ul>
<li>as_datetime</li>
</ul>
<div class="sourceCode" id="cb335"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb335-1"><a href="lubridate.html#cb335-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as_datetime</span>(<span class="st">&#39;2020-01-09 09:15:40&#39;</span>,<span class="at">tz=</span><span class="st">&#39;asia/shanghai&#39;</span>)</span>
<span id="cb335-2"><a href="lubridate.html#cb335-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-01-09 09:15:40 CST&quot;</span></span>
<span id="cb335-3"><a href="lubridate.html#cb335-3" aria-hidden="true" tabindex="-1"></a><span class="fu">as_date</span>(<span class="st">&#39;2020-01-09&#39;</span>) <span class="co">#ymd格式</span></span>
<span id="cb335-4"><a href="lubridate.html#cb335-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-01-09&quot;</span></span>
<span id="cb335-5"><a href="lubridate.html#cb335-5" aria-hidden="true" tabindex="-1"></a><span class="co"># same above</span></span>
<span id="cb335-6"><a href="lubridate.html#cb335-6" aria-hidden="true" tabindex="-1"></a><span class="co">#as_date(&#39;2020/01/09&#39;)</span></span>
<span id="cb335-7"><a href="lubridate.html#cb335-7" aria-hidden="true" tabindex="-1"></a><span class="co">#as_date(&#39;20200109&#39;)</span></span></code></pre></div>
</div>
<div id="parse-datetime" class="section level3" number="5.2.4">
<h3><span class="header-section-number">5.2.4</span> 解析日期时间</h3>
<p>数据源中日期列可能是各种的字符形式，需要转换为时间格式方便进行日期计算。商业环境中的数据是混乱的，生产库可能是不同的数据库系统，导致时间日期格式混乱，如果公司没有统一的用户层数据源，我们就需要自己清洗数据，将不同形式的日期格式转化为标准格式。</p>
<ul>
<li>解析日期</li>
</ul>
<div class="sourceCode" id="cb336"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb336-1"><a href="lubridate.html#cb336-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 整数和字符都可以</span></span>
<span id="cb336-2"><a href="lubridate.html#cb336-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="dv">20200604</span>) </span>
<span id="cb336-3"><a href="lubridate.html#cb336-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-04&quot;</span></span>
<span id="cb336-4"><a href="lubridate.html#cb336-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="st">&#39;20200604&#39;</span>)</span>
<span id="cb336-5"><a href="lubridate.html#cb336-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-04&quot;</span></span>
<span id="cb336-6"><a href="lubridate.html#cb336-6" aria-hidden="true" tabindex="-1"></a><span class="fu">mdy</span>(<span class="dv">06042020</span>)</span>
<span id="cb336-7"><a href="lubridate.html#cb336-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-04&quot;</span></span>
<span id="cb336-8"><a href="lubridate.html#cb336-8" aria-hidden="true" tabindex="-1"></a><span class="fu">dmy</span>(<span class="dv">04062020</span>)</span>
<span id="cb336-9"><a href="lubridate.html#cb336-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-04&quot;</span></span></code></pre></div>
<ul>
<li>解析时间</li>
</ul>
<div class="sourceCode" id="cb337"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb337-1"><a href="lubridate.html#cb337-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd_hm</span>(<span class="st">&quot;20100201 07-01&quot;</span>, <span class="st">&quot;20100201 07-1&quot;</span>, <span class="st">&quot;20100201 7-01&quot;</span>)</span>
<span id="cb337-2"><a href="lubridate.html#cb337-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2010-02-01 07:01:00 UTC&quot; &quot;2010-02-01 07:01:00 UTC&quot;</span></span>
<span id="cb337-3"><a href="lubridate.html#cb337-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [3] &quot;2010-02-01 07:01:00 UTC&quot;</span></span>
<span id="cb337-4"><a href="lubridate.html#cb337-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd_hms</span>(<span class="st">&quot;2013-01-24 19:39:07&quot;</span>)</span>
<span id="cb337-5"><a href="lubridate.html#cb337-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2013-01-24 19:39:07 UTC&quot;</span></span></code></pre></div>
<p>当需要处理unix时间戳时应.POSIXct()函数转化.</p>
<p><a href="https://unixtime.51240.com/">unix在线转换</a></p>
<div class="sourceCode" id="cb338"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb338-1"><a href="lubridate.html#cb338-1" aria-hidden="true" tabindex="-1"></a><span class="fu">.POSIXct</span>(<span class="dv">1591709615</span>)</span>
<span id="cb338-2"><a href="lubridate.html#cb338-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-09 21:33:35 CST&quot;</span></span>
<span id="cb338-3"><a href="lubridate.html#cb338-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd_hms</span>(<span class="fu">.POSIXct</span>(<span class="dv">1591709615</span>))</span>
<span id="cb338-4"><a href="lubridate.html#cb338-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-09 21:33:35 UTC&quot;</span></span></code></pre></div>
<p>在使用unix时间戳转换时一定注意R环境和数据系统环境时区是否一直。</p>
<blockquote>
<p>曾经我在使用阿里云的RDS数据库时没注意时区差异，导致我清洗出来的时间数据错误。</p>
</blockquote>
<div class="sourceCode" id="cb339"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb339-1"><a href="lubridate.html#cb339-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd_hms</span>(<span class="fu">.POSIXct</span>(<span class="dv">1591709615</span>),<span class="at">tz =</span> <span class="st">&#39;Asia/Shanghai&#39;</span>)</span>
<span id="cb339-2"><a href="lubridate.html#cb339-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-06-09 21:33:35 CST&quot;</span></span></code></pre></div>
</div>
<div id="extracting-datetime-information" class="section level3" number="5.2.5">
<h3><span class="header-section-number">5.2.5</span> 提取日期时间成分</h3>
<div class="sourceCode" id="cb340"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb340-1"><a href="lubridate.html#cb340-1" aria-hidden="true" tabindex="-1"></a><span class="co">#获取年</span></span>
<span id="cb340-2"><a href="lubridate.html#cb340-2" aria-hidden="true" tabindex="-1"></a><span class="fu">year</span>(<span class="fu">now</span>())  </span>
<span id="cb340-3"><a href="lubridate.html#cb340-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 2021</span></span>
<span id="cb340-4"><a href="lubridate.html#cb340-4" aria-hidden="true" tabindex="-1"></a><span class="co">#获取月</span></span>
<span id="cb340-5"><a href="lubridate.html#cb340-5" aria-hidden="true" tabindex="-1"></a><span class="fu">month</span>(<span class="fu">now</span>())</span>
<span id="cb340-6"><a href="lubridate.html#cb340-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 6</span></span>
<span id="cb340-7"><a href="lubridate.html#cb340-7" aria-hidden="true" tabindex="-1"></a><span class="co"># 当前时间所在年份天数</span></span>
<span id="cb340-8"><a href="lubridate.html#cb340-8" aria-hidden="true" tabindex="-1"></a><span class="fu">yday</span>(<span class="fu">now</span>())</span>
<span id="cb340-9"><a href="lubridate.html#cb340-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 174</span></span>
<span id="cb340-10"><a href="lubridate.html#cb340-10" aria-hidden="true" tabindex="-1"></a><span class="co"># 当前时间所在月天数</span></span>
<span id="cb340-11"><a href="lubridate.html#cb340-11" aria-hidden="true" tabindex="-1"></a><span class="fu">mday</span>(<span class="fu">now</span>())</span>
<span id="cb340-12"><a href="lubridate.html#cb340-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 23</span></span>
<span id="cb340-13"><a href="lubridate.html#cb340-13" aria-hidden="true" tabindex="-1"></a><span class="co"># 周几</span></span>
<span id="cb340-14"><a href="lubridate.html#cb340-14" aria-hidden="true" tabindex="-1"></a><span class="fu">wday</span>(<span class="fu">now</span>(),<span class="at">label =</span> <span class="cn">TRUE</span>,<span class="at">week_start =</span> <span class="dv">1</span>)</span>
<span id="cb340-15"><a href="lubridate.html#cb340-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 周三</span></span>
<span id="cb340-16"><a href="lubridate.html#cb340-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: 周一 &lt; 周二 &lt; 周三 &lt; 周四 &lt; 周五 &lt; 周六 &lt; 周日</span></span>
<span id="cb340-17"><a href="lubridate.html#cb340-17" aria-hidden="true" tabindex="-1"></a><span class="co"># 所在时刻</span></span>
<span id="cb340-18"><a href="lubridate.html#cb340-18" aria-hidden="true" tabindex="-1"></a><span class="fu">hour</span>(<span class="fu">now</span>())</span>
<span id="cb340-19"><a href="lubridate.html#cb340-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 18</span></span>
<span id="cb340-20"><a href="lubridate.html#cb340-20" aria-hidden="true" tabindex="-1"></a><span class="co"># 所在时刻</span></span>
<span id="cb340-21"><a href="lubridate.html#cb340-21" aria-hidden="true" tabindex="-1"></a><span class="fu">minute</span>(<span class="fu">now</span>())</span>
<span id="cb340-22"><a href="lubridate.html#cb340-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 15</span></span>
<span id="cb340-23"><a href="lubridate.html#cb340-23" aria-hidden="true" tabindex="-1"></a><span class="co"># 所在时刻</span></span>
<span id="cb340-24"><a href="lubridate.html#cb340-24" aria-hidden="true" tabindex="-1"></a><span class="fu">second</span>(<span class="fu">now</span>())</span>
<span id="cb340-25"><a href="lubridate.html#cb340-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 27</span></span></code></pre></div>
</div>
<div id="time-zones" class="section level3" number="5.2.6">
<h3><span class="header-section-number">5.2.6</span> 处理时区</h3>
<p>数据时区与本地R环境一致时，数据中的时区没必要处理，但是当数据是跨时区的或者不同生产系统的时区不一致，我们需要将数据时区处理一致。</p>
<p>1.时区查看</p>
<p>时区和所用系统设置相关</p>
<div class="sourceCode" id="cb341"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb341-1"><a href="lubridate.html#cb341-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Sys.timezone</span>()</span>
<span id="cb341-2"><a href="lubridate.html#cb341-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Asia/Taipei&quot;</span></span>
<span id="cb341-3"><a href="lubridate.html#cb341-3" aria-hidden="true" tabindex="-1"></a><span class="co"># windows 系统默认的时区 中国台北</span></span>
<span id="cb341-4"><a href="lubridate.html#cb341-4" aria-hidden="true" tabindex="-1"></a><span class="co"># linux 上是&quot;Asia/Shanghai&quot;</span></span>
<span id="cb341-5"><a href="lubridate.html#cb341-5" aria-hidden="true" tabindex="-1"></a><span class="co"># mac 上是&quot;Asia/Shanghai&quot;</span></span></code></pre></div>
<p>这里还有一个奇怪的点，Windows系统下时区设置为<code>(UTC+08:00)北京,重庆,香港特别行政区,乌鲁木齐</code>,但是R返回的时区是<code>Asia/Taipei</code>。</p>
<div class="sourceCode" id="cb342"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb342-1"><a href="lubridate.html#cb342-1" aria-hidden="true" tabindex="-1"></a><span class="fu">now</span>()</span>
<span id="cb342-2"><a href="lubridate.html#cb342-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:27 CST&quot;</span></span></code></pre></div>
<p><code>now()</code>输出结果中，CST是时区概念。</p>
<p>CST可以同时代表四个时间</p>
<ul>
<li>Central Standard Time (USA) UT-6:00</li>
<li>Central Standard Time (Australia) UT+9:30</li>
<li>China Standard Time UT+8:00</li>
<li>Cuba Standard Time UT-4:00</li>
</ul>
<p>2.时区调整</p>
<p>lubridate中用<code>with_tz()</code>，<code>force_tz()</code>处理时区问题</p>
<div class="sourceCode" id="cb343"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb343-1"><a href="lubridate.html#cb343-1" aria-hidden="true" tabindex="-1"></a>time <span class="ot">&lt;-</span> <span class="fu">ymd_hms</span>(<span class="st">&quot;2020-12-13 15:30:30&quot;</span>)</span>
<span id="cb343-2"><a href="lubridate.html#cb343-2" aria-hidden="true" tabindex="-1"></a>time</span>
<span id="cb343-3"><a href="lubridate.html#cb343-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-13 15:30:30 UTC&quot;</span></span>
<span id="cb343-4"><a href="lubridate.html#cb343-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb343-5"><a href="lubridate.html#cb343-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Changes printing</span></span>
<span id="cb343-6"><a href="lubridate.html#cb343-6" aria-hidden="true" tabindex="-1"></a><span class="fu">with_tz</span>(time, <span class="st">&quot;Asia/Shanghai&quot;</span>)</span>
<span id="cb343-7"><a href="lubridate.html#cb343-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-13 23:30:30 CST&quot;</span></span>
<span id="cb343-8"><a href="lubridate.html#cb343-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Changes time</span></span>
<span id="cb343-9"><a href="lubridate.html#cb343-9" aria-hidden="true" tabindex="-1"></a><span class="fu">force_tz</span>(time, <span class="st">&quot;Asia/Shanghai&quot;</span>)</span>
<span id="cb343-10"><a href="lubridate.html#cb343-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-13 15:30:30 CST&quot;</span></span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li>时区差异</li>
</ol>
<p>从下面三个时间观察时区，CST时间:中央标准时间;UTC时间:世界协调时间(UTC)是世界上不同国家用来调节时钟和时间的主要时间标准。</p>
<p>如:当UTC时间为0点时，中国CST时间为8点，因为零时区和中国北京时区相差8个时区.</p>
<ul>
<li><a href="https://home.kpn.nl/vanadovv/time/TZworld.html#asi" class="uri">https://home.kpn.nl/vanadovv/time/TZworld.html#asi</a></li>
</ul>
<div class="sourceCode" id="cb344"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb344-1"><a href="lubridate.html#cb344-1" aria-hidden="true" tabindex="-1"></a>lubridate<span class="sc">::</span><span class="fu">now</span>() <span class="co"># now函数调用系统默认时区</span></span>
<span id="cb344-2"><a href="lubridate.html#cb344-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:27 CST&quot;</span></span>
<span id="cb344-3"><a href="lubridate.html#cb344-3" aria-hidden="true" tabindex="-1"></a><span class="fu">as_datetime</span>(<span class="fu">now</span>()) <span class="co">#as_datetime默认是UTC</span></span>
<span id="cb344-4"><a href="lubridate.html#cb344-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 10:15:27 UTC&quot;</span></span>
<span id="cb344-5"><a href="lubridate.html#cb344-5" aria-hidden="true" tabindex="-1"></a><span class="fu">as_datetime</span>(<span class="fu">now</span>(),<span class="at">tz =</span> <span class="st">&#39;asia/shanghai&#39;</span>)</span>
<span id="cb344-6"><a href="lubridate.html#cb344-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-06-23 18:15:27 CST&quot;</span></span></code></pre></div>
</div>
<div id="interveal" class="section level3" number="5.2.7">
<h3><span class="header-section-number">5.2.7</span> 时间间隔</h3>
<p><code>lubridate</code>中将时间间隔保存为<code>interveal</code>类对象。</p>
<div class="sourceCode" id="cb345"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb345-1"><a href="lubridate.html#cb345-1" aria-hidden="true" tabindex="-1"></a>arrive <span class="ot">&lt;-</span> <span class="fu">ymd_hms</span>(<span class="st">&quot;2020-12-04 12:00:00&quot;</span>, <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span>)</span>
<span id="cb345-2"><a href="lubridate.html#cb345-2" aria-hidden="true" tabindex="-1"></a>arrive</span>
<span id="cb345-3"><a href="lubridate.html#cb345-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-04 12:00:00 CST&quot;</span></span>
<span id="cb345-4"><a href="lubridate.html#cb345-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb345-5"><a href="lubridate.html#cb345-5" aria-hidden="true" tabindex="-1"></a>leave <span class="ot">&lt;-</span> <span class="fu">ymd_hms</span>(<span class="st">&quot;2020-12-10 14:00:00&quot;</span>, <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span>)</span>
<span id="cb345-6"><a href="lubridate.html#cb345-6" aria-hidden="true" tabindex="-1"></a>leave</span>
<span id="cb345-7"><a href="lubridate.html#cb345-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-10 14:00:00 CST&quot;</span></span>
<span id="cb345-8"><a href="lubridate.html#cb345-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb345-9"><a href="lubridate.html#cb345-9" aria-hidden="true" tabindex="-1"></a>res <span class="ot">&lt;-</span> <span class="fu">interval</span>(arrive, leave) </span>
<span id="cb345-10"><a href="lubridate.html#cb345-10" aria-hidden="true" tabindex="-1"></a><span class="co"># same above</span></span>
<span id="cb345-11"><a href="lubridate.html#cb345-11" aria-hidden="true" tabindex="-1"></a>res <span class="ot">&lt;-</span> arrive <span class="sc">%--%</span> leave</span></code></pre></div>
<p>查看类</p>
<div class="sourceCode" id="cb346"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb346-1"><a href="lubridate.html#cb346-1" aria-hidden="true" tabindex="-1"></a><span class="fu">class</span>(res)</span>
<span id="cb346-2"><a href="lubridate.html#cb346-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Interval&quot;</span></span>
<span id="cb346-3"><a href="lubridate.html#cb346-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; attr(,&quot;package&quot;)</span></span>
<span id="cb346-4"><a href="lubridate.html#cb346-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;lubridate&quot;</span></span></code></pre></div>
<p>两个时间间隔是否重复</p>
<div class="sourceCode" id="cb347"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb347-1"><a href="lubridate.html#cb347-1" aria-hidden="true" tabindex="-1"></a>jsm <span class="ot">&lt;-</span> <span class="fu">interval</span>(<span class="fu">ymd</span>(<span class="dv">20201020</span>, <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span>), <span class="fu">ymd</span>(<span class="dv">20201231</span>, <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span>))</span>
<span id="cb347-2"><a href="lubridate.html#cb347-2" aria-hidden="true" tabindex="-1"></a>jsm</span>
<span id="cb347-3"><a href="lubridate.html#cb347-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 2020-10-20 CST--2020-12-31 CST</span></span>
<span id="cb347-4"><a href="lubridate.html#cb347-4" aria-hidden="true" tabindex="-1"></a><span class="fu">int_overlaps</span>(jsm, res)</span>
<span id="cb347-5"><a href="lubridate.html#cb347-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] TRUE</span></span></code></pre></div>
<p>更多详细用法<code>?interveal</code></p>
<div class="sourceCode" id="cb348"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb348-1"><a href="lubridate.html#cb348-1" aria-hidden="true" tabindex="-1"></a><span class="fu">interval</span>(<span class="at">start =</span> <span class="cn">NULL</span>, <span class="at">end =</span> <span class="cn">NULL</span>, <span class="at">tzone =</span> <span class="fu">tz</span>(start))</span>
<span id="cb348-2"><a href="lubridate.html#cb348-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-3"><a href="lubridate.html#cb348-3" aria-hidden="true" tabindex="-1"></a>start <span class="sc">%--%</span> end</span>
<span id="cb348-4"><a href="lubridate.html#cb348-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-5"><a href="lubridate.html#cb348-5" aria-hidden="true" tabindex="-1"></a><span class="fu">is.interval</span>(x)</span>
<span id="cb348-6"><a href="lubridate.html#cb348-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-7"><a href="lubridate.html#cb348-7" aria-hidden="true" tabindex="-1"></a><span class="fu">int_start</span>(int)</span>
<span id="cb348-8"><a href="lubridate.html#cb348-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-9"><a href="lubridate.html#cb348-9" aria-hidden="true" tabindex="-1"></a><span class="fu">int_start</span>(int) <span class="ot">&lt;-</span> value</span>
<span id="cb348-10"><a href="lubridate.html#cb348-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-11"><a href="lubridate.html#cb348-11" aria-hidden="true" tabindex="-1"></a><span class="fu">int_end</span>(int)</span>
<span id="cb348-12"><a href="lubridate.html#cb348-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-13"><a href="lubridate.html#cb348-13" aria-hidden="true" tabindex="-1"></a><span class="fu">int_end</span>(int) <span class="ot">&lt;-</span> value</span>
<span id="cb348-14"><a href="lubridate.html#cb348-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-15"><a href="lubridate.html#cb348-15" aria-hidden="true" tabindex="-1"></a><span class="fu">int_length</span>(int)</span>
<span id="cb348-16"><a href="lubridate.html#cb348-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-17"><a href="lubridate.html#cb348-17" aria-hidden="true" tabindex="-1"></a><span class="fu">int_flip</span>(int)</span>
<span id="cb348-18"><a href="lubridate.html#cb348-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-19"><a href="lubridate.html#cb348-19" aria-hidden="true" tabindex="-1"></a><span class="fu">int_shift</span>(int, by)</span>
<span id="cb348-20"><a href="lubridate.html#cb348-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-21"><a href="lubridate.html#cb348-21" aria-hidden="true" tabindex="-1"></a><span class="fu">int_overlaps</span>(int1, int2)</span>
<span id="cb348-22"><a href="lubridate.html#cb348-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-23"><a href="lubridate.html#cb348-23" aria-hidden="true" tabindex="-1"></a><span class="fu">int_standardize</span>(int)</span>
<span id="cb348-24"><a href="lubridate.html#cb348-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-25"><a href="lubridate.html#cb348-25" aria-hidden="true" tabindex="-1"></a><span class="fu">int_aligns</span>(int1, int2)</span>
<span id="cb348-26"><a href="lubridate.html#cb348-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-27"><a href="lubridate.html#cb348-27" aria-hidden="true" tabindex="-1"></a><span class="fu">int_diff</span>(times)</span></code></pre></div>
</div>
<div id="calculate-datetime" class="section level3" number="5.2.8">
<h3><span class="header-section-number">5.2.8</span> 时间日期计算</h3>
<p>时间日期计算以<code>number line</code>为依据计算。原文是<code>Because the timeline is not as reliable as the number line</code>，我没理解这句话。</p>
<div class="sourceCode" id="cb349"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb349-1"><a href="lubridate.html#cb349-1" aria-hidden="true" tabindex="-1"></a><span class="fu">minutes</span>(<span class="dv">2</span>)</span>
<span id="cb349-2"><a href="lubridate.html#cb349-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2M 0S&quot;</span></span>
<span id="cb349-3"><a href="lubridate.html#cb349-3" aria-hidden="true" tabindex="-1"></a><span class="fu">dminutes</span>(<span class="dv">2</span>)</span>
<span id="cb349-4"><a href="lubridate.html#cb349-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;120s (~2 minutes)&quot;</span></span>
<span id="cb349-5"><a href="lubridate.html#cb349-5" aria-hidden="true" tabindex="-1"></a><span class="fu">dhours</span>(<span class="dv">2</span>)</span>
<span id="cb349-6"><a href="lubridate.html#cb349-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;7200s (~2 hours)&quot;</span></span></code></pre></div>
<p>注意闰年时计算年份的差异</p>
<div class="sourceCode" id="cb350"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb350-1"><a href="lubridate.html#cb350-1" aria-hidden="true" tabindex="-1"></a><span class="fu">leap_year</span>(<span class="dv">2019</span>)</span>
<span id="cb350-2"><a href="lubridate.html#cb350-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] FALSE</span></span>
<span id="cb350-3"><a href="lubridate.html#cb350-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="dv">20190101</span>) <span class="sc">+</span> <span class="fu">dyears</span>(<span class="dv">1</span>)</span>
<span id="cb350-4"><a href="lubridate.html#cb350-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-01-01 06:00:00 UTC&quot;</span></span>
<span id="cb350-5"><a href="lubridate.html#cb350-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="dv">20190101</span>) <span class="sc">+</span> <span class="fu">years</span>(<span class="dv">1</span>)</span>
<span id="cb350-6"><a href="lubridate.html#cb350-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-01-01&quot;</span></span>
<span id="cb350-7"><a href="lubridate.html#cb350-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb350-8"><a href="lubridate.html#cb350-8" aria-hidden="true" tabindex="-1"></a><span class="fu">leap_year</span>(<span class="dv">2020</span>)</span>
<span id="cb350-9"><a href="lubridate.html#cb350-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] TRUE</span></span>
<span id="cb350-10"><a href="lubridate.html#cb350-10" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="dv">20200101</span>) <span class="sc">+</span> <span class="fu">dyears</span>(<span class="dv">1</span>)  <span class="co"># 注意查看闰年时的差异</span></span>
<span id="cb350-11"><a href="lubridate.html#cb350-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2020-12-31 06:00:00 UTC&quot;</span></span>
<span id="cb350-12"><a href="lubridate.html#cb350-12" aria-hidden="true" tabindex="-1"></a><span class="fu">ymd</span>(<span class="dv">20200101</span>) <span class="sc">+</span> <span class="fu">years</span>(<span class="dv">1</span>)</span>
<span id="cb350-13"><a href="lubridate.html#cb350-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2021-01-01&quot;</span></span></code></pre></div>
<p><code>lubridate</code>中的函数都已向量化</p>
<div class="sourceCode" id="cb351"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb351-1"><a href="lubridate.html#cb351-1" aria-hidden="true" tabindex="-1"></a>meeting <span class="ot">&lt;-</span> <span class="fu">ymd_hms</span>(<span class="st">&quot;2020-12-01 09:00:00&quot;</span>, <span class="at">tz =</span> <span class="st">&quot;asia/shanghai&quot;</span>)</span>
<span id="cb351-2"><a href="lubridate.html#cb351-2" aria-hidden="true" tabindex="-1"></a>meeting <span class="ot">&lt;-</span> meeting <span class="sc">+</span> <span class="fu">weeks</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">5</span>)</span>
<span id="cb351-3"><a href="lubridate.html#cb351-3" aria-hidden="true" tabindex="-1"></a>meeting <span class="sc">%within%</span> jsm</span>
<span id="cb351-4"><a href="lubridate.html#cb351-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE</span></span></code></pre></div>
<p>除法计算</p>
<div class="sourceCode" id="cb352"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb352-1"><a href="lubridate.html#cb352-1" aria-hidden="true" tabindex="-1"></a>res <span class="sc">/</span> <span class="fu">ddays</span>(<span class="dv">1</span>)</span>
<span id="cb352-2"><a href="lubridate.html#cb352-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 6.08</span></span>
<span id="cb352-3"><a href="lubridate.html#cb352-3" aria-hidden="true" tabindex="-1"></a>res <span class="sc">/</span> <span class="fu">dminutes</span>(<span class="dv">1</span>)</span>
<span id="cb352-4"><a href="lubridate.html#cb352-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 8760</span></span>
<span id="cb352-5"><a href="lubridate.html#cb352-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb352-6"><a href="lubridate.html#cb352-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb352-7"><a href="lubridate.html#cb352-7" aria-hidden="true" tabindex="-1"></a>res <span class="sc">%/%</span> <span class="fu">months</span>(<span class="dv">1</span>)</span>
<span id="cb352-8"><a href="lubridate.html#cb352-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 0</span></span>
<span id="cb352-9"><a href="lubridate.html#cb352-9" aria-hidden="true" tabindex="-1"></a>res <span class="sc">%%</span> <span class="fu">months</span>(<span class="dv">1</span>)</span>
<span id="cb352-10"><a href="lubridate.html#cb352-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 2020-12-04 12:00:00 CST--2020-12-10 14:00:00 CST</span></span></code></pre></div>
<p><code>as.period</code>用法</p>
<div class="sourceCode" id="cb353"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb353-1"><a href="lubridate.html#cb353-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.period</span>(res <span class="sc">%%</span> <span class="fu">months</span>(<span class="dv">1</span>))</span>
<span id="cb353-2"><a href="lubridate.html#cb353-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;6d 2H 0M 0S&quot;</span></span></code></pre></div>
<p>对于日期而言，因为月天数、年天数不一致，导致不能直接加减天数，如下：</p>
<div class="sourceCode" id="cb354"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb354-1"><a href="lubridate.html#cb354-1" aria-hidden="true" tabindex="-1"></a>jan31 <span class="ot">&lt;-</span> <span class="fu">ymd</span>(<span class="st">&quot;2020-01-31&quot;</span>)</span>
<span id="cb354-2"><a href="lubridate.html#cb354-2" aria-hidden="true" tabindex="-1"></a>jan31 <span class="sc">+</span> <span class="fu">months</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">11</span>)</span>
<span id="cb354-3"><a href="lubridate.html#cb354-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] &quot;2020-01-31&quot; NA           &quot;2020-03-31&quot; NA           &quot;2020-05-31&quot;</span></span>
<span id="cb354-4"><a href="lubridate.html#cb354-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [6] NA           &quot;2020-07-31&quot; &quot;2020-08-31&quot; NA           &quot;2020-10-31&quot;</span></span>
<span id="cb354-5"><a href="lubridate.html#cb354-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [11] NA           &quot;2020-12-31&quot;</span></span></code></pre></div>
<p><code>lubridate</code>中不存在的日期返回<code>NA</code></p>
<p>解决方案是：<code>%m+%</code>或<code>%m-%</code></p>
<div class="sourceCode" id="cb355"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb355-1"><a href="lubridate.html#cb355-1" aria-hidden="true" tabindex="-1"></a>jan31 <span class="sc">%m+%</span> <span class="fu">months</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">11</span>)</span>
<span id="cb355-2"><a href="lubridate.html#cb355-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] &quot;2020-01-31&quot; &quot;2020-02-29&quot; &quot;2020-03-31&quot; &quot;2020-04-30&quot; &quot;2020-05-31&quot;</span></span>
<span id="cb355-3"><a href="lubridate.html#cb355-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [6] &quot;2020-06-30&quot; &quot;2020-07-31&quot; &quot;2020-08-31&quot; &quot;2020-09-30&quot; &quot;2020-10-31&quot;</span></span>
<span id="cb355-4"><a href="lubridate.html#cb355-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [11] &quot;2020-11-30&quot; &quot;2020-12-31&quot;</span></span>
<span id="cb355-5"><a href="lubridate.html#cb355-5" aria-hidden="true" tabindex="-1"></a>jan31 <span class="sc">%m-%</span> <span class="fu">months</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">11</span>)</span>
<span id="cb355-6"><a href="lubridate.html#cb355-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] &quot;2020-01-31&quot; &quot;2019-12-31&quot; &quot;2019-11-30&quot; &quot;2019-10-31&quot; &quot;2019-09-30&quot;</span></span>
<span id="cb355-7"><a href="lubridate.html#cb355-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [6] &quot;2019-08-31&quot; &quot;2019-07-31&quot; &quot;2019-06-30&quot; &quot;2019-05-31&quot; &quot;2019-04-30&quot;</span></span>
<span id="cb355-8"><a href="lubridate.html#cb355-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [11] &quot;2019-03-31&quot; &quot;2019-02-28&quot;</span></span></code></pre></div>
</div>
</div>
<div class="footnotes">
<hr />
<ol start="10">
<li id="fn10"><p>lubridate包中大部分函数默认时区为“UTC”，在涉及时间处理时需要注意时区。<a href="lubridate.html#fnref10" class="footnote-back">↩︎</a></p></li>
</ol>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="datetime-base-R.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="datetime-application.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["Data-Handling-in-R.pdf", "Data-Handling-in-R.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
